﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace ActivityFeed.SharePoint.Sample.ActivityFeedWebPart
{
    [ToolboxItemAttribute(false)]
    public class ActivityFeedWebPart : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/ActivityFeed.SharePoint.Sample/ActivityFeedWebPart/ActivityFeedWebPartUserControl.ascx";

        protected override void OnInit(EventArgs e)
        {
            ScriptLink.Register(this.Page, "ActivityFeedWebPart/Scripts/jquery.js", false);
            ScriptLink.Register(this.Page, "ActivityFeedWebPart/Scripts/json2.js", false);
            ScriptLink.Register(this.Page, "ActivityFeedWebPart/Scripts/knockout-2.1.0.js", false);
            ScriptLink.Register(this.Page, "ActivityFeedWebPart/Scripts/knockout.mapping-latest.js", false);

            ScriptLink.Register(this.Page, "ActivityFeedWebPart/Scripts/moment.js", false);
            ScriptLink.Register(this.Page, "ActivityFeedWebPart/Scripts/activityFeed.js", false);

            this.RegisterCss("font-awesome.css");
            this.RegisterCss("activityFeed.css");
            this.RegisterCss("activityFeed-ie-fallback.css", "gte IE 9");
            this.RegisterCss("activityFeed-ie8-fallback.css", "IE 8");

            base.OnInit(e);
        }

        protected override void CreateChildControls()
        {
            var control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }

        private void RegisterCss(string cssName)
        {
            this.RegisterCss(cssName, null);
        }

        private void RegisterCss(string cssName, string conditionalExpression)
        {
            var cssRegistration = new CssRegistration
            {
                Name = "/_layouts/ActivityFeedVisualTest/Styles/" + cssName,
            };

            if (!string.IsNullOrEmpty(conditionalExpression))
            {
                cssRegistration.ConditionalExpression = conditionalExpression;
            }

            Controls.Add(cssRegistration);
        }
    }
}
